
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div class="convertImage">
    <div class="title">
        Json 工具
    </div>
    <div class="content">
        <div id="editor" style="height:600px; height:800px;">
            <pre>
                package com.saoft.fangcms.generator;

            import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
            import com.baomidou.mybatisplus.core.toolkit.StringPool;
            import com.baomidou.mybatisplus.core.toolkit.StringUtils;
            import com.baomidou.mybatisplus.generator.AutoGenerator;
            import com.baomidou.mybatisplus.generator.InjectionConfig;
            import com.baomidou.mybatisplus.generator.config.*;
            import com.baomidou.mybatisplus.generator.config.po.TableInfo;
            import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
            import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

            import java.util.*;

            // 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
            public class CodeGenerator {

            private static String commonParent = "com.saoft.fangcms";

            /**
            * <p>
            * 读取控制台内容
            * </p>
            */
            public static String scanner(String tip) {
            Scanner scanner = new Scanner(System.in);
            StringBuilder help = new StringBuilder();
            help.append("请输入" + tip + "：");
            System.out.println(help.toString());
            if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
            return ipt;
            }
            }
            throw new MybatisPlusException("请输入正确的" + tip + "！");
            }

            /**
            * controller 输出路径
            * @param pc
            * @param projectPath
            * @return
            */
            public static String getControllerPath(PackageConfig pc,String projectPath){

            StringBuffer fileName = new StringBuffer();
            fileName.append(projectPath);
            fileName.append("/src/main/java/");
            fileName.append(commonParent.replaceAll("\\.","/"));
            fileName.append("/web/controller/");
            fileName.append(pc.getModuleName());
            return fileName.toString();
            }


            /**
            * jsp 输出路径
            * @param pc
            * @param projectPath
            * @return
            */
            public static String getJspPath(PackageConfig pc,String projectPath){
            StringBuffer fileName = new StringBuffer();
            fileName.append(projectPath);
            fileName.append("/src/main/webapp/WEB-INF/view/");
            fileName.append("/");
            fileName.append(pc.getModuleName());
            return fileName.toString();
            }


            public static void main(String[] args) {
            // 代码生成器
            AutoGenerator mpg = new AutoGenerator();

            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor("scc95599");
            gc.setOpen(false);
            gc.setActiveRecord(true);
            gc.setFileOverride(true);
            gc.setServiceName("%sService");
            // gc.setSwagger2(true); 实体属性 Swagger2 注解
            mpg.setGlobalConfig(gc);

            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql://localhost:3306/fangcms?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT");
            // dsc.setSchemaName("public");
            dsc.setDriverName("com.mysql.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123456");
            mpg.setDataSource(dsc);

            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setModuleName(scanner("模块名"));
            pc.setParent(commonParent+".modules");
            mpg.setPackageInfo(pc);

            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();

            // 配置自定义输出模板
            //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
            // templateConfig.setEntity("templates/entity2.java");
            // templateConfig.setService();
            templateConfig.setController(null);

            //        templateConfig.setXml(null);
            mpg.setTemplate(templateConfig);

            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);

            strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
            strategy.setControllerMappingHyphenStyle(true);
            strategy.setTablePrefix(pc.getModuleName() + "_");

            String scanner = scanner("是否生成前端代码?Y/N");
            if (scanner != null && scanner.toUpperCase().equals("Y")) {
            templateConfig.setEntity(null);
            templateConfig.setMapper(null);
            templateConfig.setService(null);
            templateConfig.setServiceImpl(null);
            templateConfig.setXml(null);
            generatorFrontCode(mpg,projectPath);
            }
            mpg.setStrategy(strategy);
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
            mpg.execute();
            }


            /**
            * 生成4个文件  controller.java index.jsp
            *            add.jsp detail.jsp
            *
            * @param mpg
            * @param projectPath
            */
            private static void generatorFrontCode(AutoGenerator mpg,String projectPath){

            PackageConfig pc = mpg.getPackageInfo();
            // 自定义配置
            InjectionConfig cfg = new InjectionConfig() {

            private Map<String, Object> selfMap = new HashMap<>();
            @Override
            public void initMap() {

            }

            @Override
            public void initTableMap(TableInfo tableInfo) {
            String name = StringUtils.firstToLowerCase(tableInfo.getEntityName());
            String underline = StringUtils.camelToUnderline(tableInfo.getEntityName());
            selfMap.put("name", name);
            selfMap.put("title", tableInfo.getComment());

            String moduleName = pc.getModuleName();
            selfMap.put("requestMapping", "/" + moduleName + "/" + underline);
            selfMap.put("baseUrl", "/" + moduleName + "/" + underline);

            selfMap.put("permissions", moduleName + ":" + underline);

            selfMap.put("controllerPkg", commonParent+".web.controller."+moduleName);
            }

            @Override
            public Map<String, Object> prepareObjectMap(Map<String, Object> objectMap) {
            selfMap = objectMap;
            return super.prepareObjectMap(objectMap);
            }
            };

            // 自定义输出配置pr
            List<FileOutConfig> focList = new ArrayList<>();
            List<FileOutConfig> focList2 = new ArrayList<>();
                // 自定义配置会被优先输出
                focList.add(new FileOutConfig("/generator/controller.java.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return getControllerPath(pc, projectPath)
                + "/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
                }
                });

                focList.add(new FileOutConfig("/generator/index.jsp.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return getJspPath(pc, projectPath)
                + "/" + underlineName(tableInfo) + "/index.jsp";
                }
                });

                focList.add(new FileOutConfig("/generator/add.jsp.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return getJspPath(pc, projectPath)
                + "/" + underlineName(tableInfo) + "/add.jsp";
                }
                });

                focList2.add(new FileOutConfig("/generator/detail.jsp.ftl") {
                @Override
                public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return getJspPath(pc, projectPath)
                + "/" + underlineName(tableInfo) + "/detail.jsp";
                }
                });


                /*
                cfg.setFileCreate(new IFileCreate() {
                @Override
                public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
                }
                });
                */
                cfg.setFileOutConfigList(focList);
                mpg.setCfg(cfg);
                }

                private static String underlineName(TableInfo tableInfo){
                return StringUtils.camelToUnderline(tableInfo.getEntityName());
                }

                }
            </pre>
        </div>
        <div class="btn-group button" role="group" aria-label="...">
            <button type="button" class="btn btn-default" @click="jsonFormat">Json 格式化（format)</button>
            <button type="button" class="btn btn-default" @click="jsonEncode">Json 压缩</button>
        </div>
    </div>
</div>



<script src="https://cdn.bootcss.com/ace/1.4.6/ace.js"></script>
<script src="https://cdn.bootcss.com/ace/1.4.6/ext-beautify.js"></script>
<script src="https://cdn.bootcss.com/ace/1.4.6/ext-language_tools.js"></script>
<script src="https://cdn.bootcss.com/ace/1.4.6/mode-java.js"></script>
<script src="https://cdn.bootcss.com/ace/1.4.6/theme-dracula.js"></script>

<script>
    var langagueTools = require("ace/ext/language_tools");
    this.editor = ace.edit('editor');// 这个地方就是id是editor的div
    this.editor.setTheme('ace/theme/dracula');
    this.editor.session.setMode('ace/mode/java');
</script>

</body>
</html>
